package com.hx.hospital.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hx.common.result.PageResult;
import com.hx.hospital.mapper.FloorMapper;
import com.hx.hospital.pojo.Floor;
import com.hx.hospital.service.FloorService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class FloorServiceImpl implements FloorService {

    @Autowired
    private FloorMapper floorMapper;

    /**
     * 返回全部记录
     * @return
     */
    public List<Floor> findAll() {
        return floorMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<Floor> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<Floor> floors = (Page<Floor>) floorMapper.selectAll();
        return new PageResult<Floor>(floors.getTotal(),floors.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<Floor> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return floorMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<Floor> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<Floor> floors = (Page<Floor>) floorMapper.selectByExample(example);
        return new PageResult<Floor>(floors.getTotal(),floors.getResult());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public Floor findById(Long id) {
        return floorMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param floor
     */
    public void add(Floor floor) {
        floorMapper.insert(floor);
    }

    /**
     * 修改
     * @param floor
     */
    public void update(Floor floor) {
        floorMapper.updateByPrimaryKeySelective(floor);
    }

    /**
     *  删除
     * @param id
     */
    public void delete(Long id) {
        floorMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Floor.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 楼层
            if(searchMap.get("floor")!=null && !"".equals(searchMap.get("floor"))){
                criteria.andLike("floor","%"+searchMap.get("floor")+"%");
            }
            // department_office
            if(searchMap.get("departmentOffice")!=null && !"".equals(searchMap.get("departmentOffice"))){
                criteria.andLike("departmentOffice","%"+searchMap.get("departmentOffice")+"%");
            }


        }
        return example;
    }

}
